﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using System.Web.Security;
using Htss.Platform.Web;
using Htss.Platform.Core;
namespace DreamBox.Web.Box
{
    public class loginBox:ControllerBox 
    {
        public ActionResult Load()
        {
            if (Session["UserID"] != null)
            {
                BLL.Message.Jump(this, "您已经登录,请勿重复登录!", "/", "0");
            }
            if (Request.RequestType == "POST"&&Verification())
            {
                string UserName = Request.Form["UserName"];
                string Password = Request.Form["Password"];
                string IsCookie = Request.Form["IsCookie"];

                DAL.User.Login login = new DAL.User.Login();

                DAL.PSelect ps = new DAL.PSelect();
                DataTable dt =login.UserLogin(UserName,FormsAuthentication.HashPasswordForStoringInConfigFile(Password, "MD5"));
                if (dt.Rows.Count > 0)
                {
                    if (!bool.Parse(dt.Rows[0]["IsStatus"].ToString()))
                    {
                        BLL.Message.Jump(this, "您的账号已经被锁定,请联系网站管理员!", "/login", "0");
                    }

                    if (IsCookie != "" && IsCookie == "true")
                    {
                        BLL.User.Cookies.Add(this.Response, UserName, FormsAuthentication.HashPasswordForStoringInConfigFile(Password, "MD5"));
                    }
                    //存储用户基本信息
                    this.Session["UserID"] = dt.Rows[0]["UserID"];
                    this.Session["UserName"] = dt.Rows[0]["UserName"];
                    this.Session["Avatar"] = dt.Rows[0]["Avatar"];
                    this.Session["Location"] = dt.Rows[0]["Location"];
                    //清空不登录时的权限列表信息
                    if (this.Session["GroupCompetenceData"] != null)
                    {
                        this.Session.Remove("GroupCompetenceData");
                    }
                    //更新登录后的用户组信息
                    BLL.User.Competence.ComGroupRight group = new BLL.User.Competence.ComGroupRight();
                    group.SetCompetence(System.Web.HttpContext.Current, dt);
                    BLL.Message.Jump(this, "登录成功,即将进入首页!", "/", "1");
                }
                else
                {
                    BLL.Message.Jump(this, "您输入的账号或密码有误,请重新登录!", "/login", "0");
                }
            }
            return View("login");
        }

        //验证
        bool Verification()
        {
            Verify vf = new Verify();
            vf.VerificationMessage = VerificationMessage;
            VerifyField vUserName = vf.NewVerifyField(Request.Form["UserName"], "UserName");
            vUserName.AddNull("请输入用户名");
            vUserName.AddLengthRange("长度范围在1-20之间", 1, 20);

            VerifyField vPassword = vf.NewVerifyField(Request.Form["Password"], "Password");
            vPassword.AddNull("此项不能为空");
            vPassword.AddLengthRange("请输入(6-20)之间的密码长度", 6, 20);

      

            vf.Begin();
            return vf.IsVerify;
        }
    }
}
